package uml.GUI;

import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import net.miginfocom.swing.MigLayout;

import uml.controller.Controller;
import uml.eventsystem.IObserver;
import uml.eventsystem.MouseHandler;
import uml.eventsystem.events.UMLEvent;
import uml.eventsystem.events.UMLEventId;
import uml.util.UMLConstants;

public class MainWindow
		extends JFrame
		implements IObserver, WindowListener
{
	private CommandRibbon m_menu;
	private ControlPalatte m_buttonPalatte;
	//private DiagramPanel m_diagramPanel;
	private UMLTabbedPane m_diagramPanel;
	private static final long serialVersionUID = 7939492728719590916L;
	private final String WINDOWTITLE = "A-Team UML Editor";

	/**
	 * Initializes the window and sets up the gui
	 * 
	 * @param controller
	 */
	public MainWindow (Controller controller)
	{
		super ();
		initWindow (controller);
		initGui (controller);
		this.addWindowListener(this);
	}

	/**
	 * Handles setting up the gui
	 * 
	 * @param controller
	 */
	private void initGui (Controller controller)
	{
		Container contentPane = this.getContentPane ();
		contentPane.setBackground (UMLConstants.WINDOW_BACKGROUND);
		contentPane.setLayout (new MigLayout ("wrap 2, fillx", "[grow] []", "[grow]"));

		m_diagramPanel = new UMLTabbedPane ();
		//m_diagramPanel.addFocusListener(this);
		contentPane.add (m_diagramPanel, "growx, growy");

		m_menu = new CommandRibbon (m_diagramPanel);
		this.setJMenuBar (m_menu);

		m_buttonPalatte = new ControlPalatte (m_diagramPanel);
		contentPane.add (m_buttonPalatte, "growy, width 100, east");

		this.validate ();
	}

	/**
	 * Sets up the window
	 * 
	 * @param controller
	 */
	private void initWindow (Controller controller)
	{
		setTitle (WINDOWTITLE);
		setDefaultCloseOperation (EXIT_ON_CLOSE);
		setIconImage (Toolkit.getDefaultToolkit ().getImage ("a-team-van-icon.png"));

		setSize (800, 600);
		setVisible (true);
	}

	@Override
	public void handleNotification (UMLEvent event)
	{
		if (event.getEventId ().equals (UMLEventId.APPLICATION_EXIT))
		{
			this.dispose ();
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0)
	{
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) 
	{
		
	}

	@Override
	public void windowClosing(WindowEvent arg0)
	{
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) 
	{
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) 
	{
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) 
	{
		MouseHandler.getMouseHandler().setContextMenuVisible(false);
	}

	@Override
	public void windowOpened(WindowEvent arg0) 
	{
		
	}
}
